<template>
  <div>
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="技术支持" name="first">
          <el-form :model="serviceForm" ref="serviceForm" size="small" style="width:400px;">
            <el-form-item label="标题" prop="notice_title" label-width="100px">
              <el-input v-model="serviceForm.title"></el-input>
            </el-form-item>
            <el-form-item label="文字1" prop="notice_title" label-width="100px">
              <el-input v-model="serviceForm.txt1"></el-input>
            </el-form-item>
            <el-form-item label="文字2" prop="notice_title" label-width="100px">
              <el-input v-model="serviceForm.txt2"></el-input>
            </el-form-item>
            <el-form-item label="图片" prop="notice_title" label-width="100px">
              <el-upload
                class="avatar-uploader"
                :action="uploadAction"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload">
                <img v-if="serviceForm.imageUrl" :src="serviceForm.imageUrl" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
            <el-form-item label-width="100px">
              <el-button type="primary" @click="onSubmit">添加设置</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
export default {
    data() {
      return {
        activeName: 'first',
        serviceForm:{
          imageUrl:''
        },
        uploadAction:''
      }
    },
    created() {
      console.log(process.env.NODE_ENV);
      if(process.env.NODE_ENV == 'development') {
        this.uploadAction = 'http://localhost:8081/api/admin/index/uploadFile?type=0'
      } else {
        this.uploadAction = 'https://saas.jzkj.net/admin/index/uploadFile?type=0'
      }
      this.getSetting()
    },
    methods: {
      // 获取设置
      getSetting() {
        this.$axios.post(this.API.SettingGet).then(res => {
          this.serviceForm = JSON.parse(res.data.data.list[0].content)
        })
      },
      // 提交设置
      onSubmit() {
        let data = {
          type:1,
          content:JSON.stringify(this.serviceForm)
        }
        this.$axios.post(this.API.SettingSet,data).then(res => {
          this.$message({type: "success",message: "设置成功",});
          this.getSetting()
        })
      },
      handleClick(tab, event) {
      },
      handleAvatarSuccess(res, file) {
        this.serviceForm.imageUrl = res.data.url
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg' || 'image/png';
        const isLt2M = file.size / 1024 / 1024 < 3;

        if (!isJPG) {
          this.$message.error('上传图片只能是 JPG/PNG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传图片大小不能超过 3MB!');
        }
        return isJPG && isLt2M;
      }
    }
}
</script>

<style lang="scss" scoped>
  .el-input {
    width: 300px;
  }
  .avatar-uploader /deep/.el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 150px;
    height: 150px;
    line-height: 150px;
    text-align: center;
  }
  .avatar {
    width: 150px;
    height: 150px;
    display: block;
  }
</style>